import { Component } from "@angular/core";
import { NavController,NavParams,AlertController } from 'ionic-angular';
import { HttpClient,HttpHeaders } from '@angular/common/http'
import { NativeStorage } from '@ionic-native/native-storage';
import { TranslateService } from '@ngx-translate/core';

import { AppConfig } from '../../../app/app.config';
@Component({
    selector: 'page-contact',
    templateUrl: 'look.html',
})

export class LookPage{
    detail:any;
    src=AppConfig.getMainUrl()
    form={
        pid: 0,
        truename:'',
        contacttime:'',
        email:'',
        tel:'',
        message:''
    };
    constructor(
        private navCtl:NavController,
        private http:HttpClient,
        private stroge:NativeStorage,
        private navParams:NavParams,
        private alertCtrl:AlertController,
        private translate: TranslateService
    ){
        this.detail ={
            id:1,
            title:'China',
            lat:43.0741904,
            lng:-88.3809802,
            type:2,
            price:123,
        }
        translate.setDefaultLang('en')
    }
    ionViewDidEnter(){
        this.stroge.getItem('lang').then(res=>{
            this.translate.use(res)
        })
        this.stroge.getItem('token').then(token=>{
            this.http.get(AppConfig.getUrl()+'user/getInfo?token='+token).subscribe((res)=>{
                if(res['code']==101){
                    this.form.truename = res['data']['firstname']
                    this.form.email = res['data']['email']
                    this.form.tel = res['data']['mobile']
                }
            })
        })
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    ionViewDidLoad(){
        this.detail = this.navParams.get('data');
        this.form.pid = this.detail.id;
        switch(this.detail.area_unit){
            case 1:
                this.detail.area_unit='SQ.FT'
                this.detail.other_area = Math.ceil(this.detail.area / 10.764);
                this.detail.other_area_unit='SQ.M'
                break
            case 2:
                this.detail.area_unit='SQ.M'
                this.detail.other_area = Math.ceil(this.detail.area * 10.764);
                this.detail.other_area_unit='SQ.FT'
                break
          }
          switch(this.detail.cate){
            case 1:
              this.detail.cateName='Rent'
              break
            case 2:
              this.detail.cateName='Buy'
              break
            case 3:
              this.detail.cateName='Rent per day'
              break
            case 4:
              this.detail.cateName='Service'
              break
          }
          switch(this.detail.price_unit){
            case 1:
            this.detail.price_unit='£'
              break
            case 2:
            this.detail.price_unit='€'
              break
            case 3:
            this.detail.price_unit='$'
              break
            case 4:
            this.detail.price_unit='¥'
              break
          }
          switch(this.detail.price_type){
            case 1:
            this.detail.price_type='Month'
              break
            case 2:
            this.detail.price_type='Week'
              break
            case 3:
            this.detail.price_type='Day'
              break
          }
    }
    sub(){
        this.stroge.getItem('token').then(token=>{
            this.http.post(AppConfig.getUrl()+'house/view?token='+token, AppConfig.urlEcode(this.form), { headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( res => {
                if(res['code']===101){
                    this.alertb('','提交成功');
                    this.navCtl.pop();
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
        })
       
    }
    
}